package com.mjk.manager.amis.core.controller;

import com.mjk.common.base.constns.BeeCommonConstns;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.manager.amis.core.dto.AmisCrudDTO;
import com.mjk.manager.amis.core.dto.SessionDetails;
import com.mjk.manager.amis.core.model.SysPage;
import com.mjk.manager.amis.core.utils.SessionHolder;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/mamis/sys/page")
@RestController
@Slf4j
public class SysPageController {
	@Autowired
	SessionHolder contextService;

	@Autowired
    BeeDBService beeDBService;

	/**
	 * 获取动态 amis 页面列表
	 */
	@GetMapping("/list")
	public BeeResponse getPageList(final HttpServletRequest request,
                                   @RequestParam(name = "orderBy", required = false) final String orderBy,
                                   @RequestParam(name = "orderDir", required = false) final String orderDir) {
		// 查询数据库
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new SysPage());
		wrapper.eq(SysPage::getDELETED, BeeCommonConstns.DEFAULT_UNDEL);
		List<SysPage> items = beeDBService.findManyByWrapper(wrapper);

		// 构造 amis 要求的数据结构
		final AmisCrudDTO<SysPage> data = new AmisCrudDTO<>();
		data.setItems(items);

		// 返回结果集
		return BeeError.BEE_SUC.tranceError(data);
	}

	@GetMapping("/{id}")
	public BeeResponse getPage(final HttpServletRequest request, @PathVariable("id") String id) {
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new SysPage());
		wrapper.eq(SysPage::getId, id);
		List<SysPage> pages = beeDBService.findManyByWrapper(wrapper);
		if (CollectionUtils.isEmpty(pages)) {
			return BeeError.SYSTEM_RETURN_DATA_IS_NULL.tranceError();
		}
		return BeeError.BEE_SUC.tranceError(pages.get(0));
	}

	@PostMapping()
	public BeeResponse savePage(final HttpServletRequest request, @RequestBody final SysPage obj) {
		final SessionDetails session = contextService.getSession(request);

		obj.setUPDATED_BY(session.getCurUser().getId());

		int ret;
		if (!ObjectUtils.isEmpty(obj.getId())) {
			// update
			obj.setUPDATED_AT(LocalDateTime.now());
			BeeQueryWrapper wrapper = new BeeQueryWrapper(obj);
			wrapper.eq(SysPage::getId, obj.getId());
			ret = beeDBService.update(wrapper);
		} else {
			// insert
			obj.setDELETED(BeeCommonConstns.DEFAULT_UNDEL);
			obj.setCREATED_BY(session.getCurUser().getId());
			obj.setCREATED_AT(LocalDateTime.now());
			obj.setUPDATED_AT(LocalDateTime.now());

			ret = beeDBService.save(obj);
		}

		return ret > 0 ? BeeError.BEE_SUC.tranceError() : BeeError.BEE_FAILD.tranceError();
	}

	@DeleteMapping("/{id}")
	public BeeResponse delPage(final HttpServletRequest request, @PathVariable("id") String id) {
		SysPage page = new SysPage();
		page.setDELETED(BeeCommonConstns.DEFAULT_DEL);
		BeeQueryWrapper wrapper = new BeeQueryWrapper(page);
		wrapper.eq(SysPage::getId, id);
		int ret = beeDBService.update(wrapper);
		return ret > 0 ? BeeError.BEE_SUC.tranceError() : BeeError.BEE_FAILD.tranceError();
	}

	@GetMapping("/option/list")
	public BeeResponse getPageOptionList(final HttpServletRequest request) {
		// 构建查询
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new SysPage());

		final List<SysPage> pageList = beeDBService.findManyByWrapper(wrapper);
		pageList.stream().forEach(item -> {
			item.setLabel(item.getNAME());
			item.setValue(item.getId());
		});
		// 返回结果集
		return BeeError.BEE_SUC.tranceError(pageList);
	}
}
